package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.entity.bo.UsedGradeRuleBo;
import com.nuoniu.sibanyun.entity.erp.ErpSettleStyle;
import com.nuoniu.sibanyun.mapper.erp.ErpSettleStyleMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpGradeRuleService;
import com.nuoniu.sibanyun.service.erp.IErpSettleStyleService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 结算方式设置 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-10-12
 */
@Service
public class ErpSettleStyleServiceImpl extends ServiceImpl<ErpSettleStyleMapper, ErpSettleStyle> implements IErpSettleStyleService {

    @Autowired
    private IErpGradeRuleService erpGradeRuleService;
    @Autowired
    private IErpAccountService accountService;

    @Override
    public Page<ErpSettleStyle> pageList(Page pages, ErpSettleStyle settleStyle) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpSettleStyle> queryWrapper = new QueryWrapper<>(settleStyle);
        queryWrapper.eq("company_id", userInfo.getCompanyId());
        queryWrapper.orderByAsc("code");
        Page page = page(pages, queryWrapper);
        return page;
    }

    @Override
    public void saveSettyleStyle(ErpSettleStyle settleStyle) {
        UserInfo userInfo = UserThreadLocal.get();
        ErpSettleStyle settleStyleByName = baseMapper.getByName(userInfo.getCompanyId(), settleStyle.getName());
        ErpSettleStyle settleStyleByCode = baseMapper.getByCode(userInfo.getCompanyId(), settleStyle.getCode());
        //判断结算方式编码规则
        UsedGradeRuleBo usedGradeRuleBo = erpGradeRuleService.checkCode(settleStyle.getCode(), null, null, "settlestyle");
        settleStyle.setGradeRuleLevel(usedGradeRuleBo.getGradeRuleLevel());
        settleStyle.setGradeRuleStr(usedGradeRuleBo.getGradeRuleStr());
        settleStyle.setCompanyId(userInfo.getCompanyId());
        if(settleStyleByName != null && (settleStyle.getId() == null || !settleStyleByName.getId().equals(settleStyle.getId()))) {
            throw new NuoNiuException("名称不能重复");
        }
        if(settleStyleByCode != null && (settleStyle.getId() == null || !settleStyleByCode.getId().equals(settleStyle.getId()))) {
            throw new NuoNiuException("编码不能重复");
        }

        if(settleStyle.getId() != null) {
            settleStyle.setUpdateDate(LocalDateTime.now());
            settleStyle.setUpdateUserId(userInfo.getUserId());
        }else {
            settleStyle.setAccountId(accountService.getCurrentAccountId());
            settleStyle.setCreateUserId(userInfo.getUserId());
        }
        this.saveOrUpdate(settleStyle);
    }
}
